/**
 * 
 * Author：liaozhanggen
 * Create Date：2017年3月21日
 * Version：v2.0
 */
package com.choosefine.base.shixiaobao.common;

import java.io.InputStream;

import org.mockito.Mock;
import org.springframework.test.web.servlet.MockMvc;

import com.choosefine.base.shixiaobao.common.def.ResultCode;
import com.choosefine.base.shixiaobao.common.json.JsonDataResult;
import com.choosefine.base.shixiaobao.common.json.JsonResult;
import com.choosefine.base.shixiaobao.common.utils.MessageSourceUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;

/**
 * 
 * Author：liaozhanggen
 * Create Date：2017年3月21日
 * Version：v2.0
 */
public class BaseTestCase {

	@Mock
	public MessageSourceUtils messageSourceUtils; // 国际化读取key/value

	public MockMvc mockMvc;

	public ObjectMapper mapper = new ObjectMapper();
    /**
	 * 根据当前包下的文件资源
	 * 
	 * @Author：liaozhanggen
	 * @Create Date：2017年3月21日
	 * @param：fileName
	 * @return：InputStream
	 */
	public InputStream getClassPathResource(String fileName) {
		return Thread.currentThread().getContextClassLoader().getResourceAsStream("json".concat(fileName));
	}

	private JsonResult result = null;

	public JsonResult responseSuccess(Object data, String MESSAGE, Integer resultCode) {
		result = new JsonResult(data, MESSAGE, resultCode);
		return result;
	}

	public String responseSuccess() {
		this.responseSuccess(null, ResultCode.MSG_SUCCESS, ResultCode.SUCCESS);
		return result.toJson();
	}

	public String responseSuccess(Object data) {
		if (data instanceof Page) {
			JsonDataResult<Object> jdr = new JsonDataResult<>();
			jdr.setList((Page) data);
			jdr.setPages(((Page) data).toPageInfo().getPages());
			jdr.setTotal(((Page) data).toPageInfo().getTotal());
			jdr.setPageSize(((Page) data).toPageInfo().getPageSize());
			jdr.setPageNum(((Page) data).toPageInfo().getPageNum());
			this.responseSuccess(jdr, ResultCode.MSG_SUCCESS, ResultCode.SUCCESS);
		} else {
			this.responseSuccess(data, ResultCode.MSG_SUCCESS, ResultCode.SUCCESS);
		}
		return result.toJson();
	}

	public String responseSuccess(Object data, String MESSAGE) {
		result = new JsonResult(data, MESSAGE, ResultCode.SUCCESS);
		return result.toJson();
	}

	public String responseSuccess(Object data, Integer resultCode, String MESSAGE) {
		result = new JsonResult(data, MESSAGE, resultCode);
		return result.toJson();
	}

	public String responseFail() {
		result = new JsonResult(ResultCode.MSG_ERROR, ResultCode.FAILED);
		return result.toJson();
	}

	public String responseFail(String MESSAGE) {
		result = new JsonResult(MESSAGE, ResultCode.FAILED);
		return result.toJson();
	}

	public String responseFail(Object data, String faileMsg) {
		result = new JsonResult(data, faileMsg, ResultCode.FAILED);
		return result.toJson();
	}

	public String responseFail(Object data) {
		result = new JsonResult(data, ResultCode.MSG_ERROR, ResultCode.FAILED);
		return result.toJson();
	}

	public String responseFail(Integer resultCode, String MESSAGE) {
		result = new JsonResult(MESSAGE, resultCode);
		return result.toJson();
	}

}
